package com.gip.controller;

import com.gip.mapper.StorageMapper;
import com.gip.pojo.*;
import com.gip.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class MenuController {
    @Autowired
    EmployeeService employeeService;

    @Autowired
    SupplierService supplierService;

    @Autowired
    StorageMapper storageMapper;

    @Autowired
    ProductService productService;

    @Autowired
    CustomerService customerService;

    @Autowired
    InorderService inorderService;

    //供货商列表
    @RequestMapping("/supplierTable")
    public String supplierTable() {
        return "supplierTable";
    }

    //供货商添加
    @RequestMapping("/supplierAdd")
    public String supplierAdd() {
        return "supplierAdd";
    }

    //供货商编辑
    @RequestMapping("/supplierEdit")
    public String supplierEdit(int id, Model model) {
        Supplier supplierById = supplierService.findSupplierById(id);
        model.addAttribute("supplier", supplierById);
        return "supplierEdit";
    }



    //客户列表
    @RequestMapping("/customerTable")
    public String customerTable() {
        return "customerTable";
    }

    //客户编辑
    @RequestMapping("/customerEdit")
    public String customerEdit(int id, Model model) {
        Customer customer = customerService.findAllCustomerByIf(Customer.builder().customerId(id).build()).stream().findFirst().get();
        model.addAttribute("customer", customer);
        return "customerEdit";
    }

    //客户添加
    @RequestMapping("/customerAdd")
    public String customerAdd() {
        return "customerAdd";
    }

    //用户列表
    @RequestMapping("/employeeTable")
    public String employeeTable() {
        return "employeeTable";
    }

    @RequestMapping("/inOrderTable")
    public String inOrderTable() {
        return "inOrderTable";
    }

    @RequestMapping("/inOrderAdd")
    public String inOrderAdd(Model model) {
        List<Supplier> allSuppliers = supplierService.findAllSuppliers();
        model.addAttribute("allSuppliers", allSuppliers);
        List<Storage> allStorage = storageMapper.findAllStorage();
        model.addAttribute("allStorage", allStorage);
        List<Product> allProduct = productService.findAllProduct();
        model.addAttribute("allProduct", allProduct);
        return "inOrderAdd";
    }

    @RequestMapping("/outOrderTable")
    public String outOrderTable() {
        return "outOrderTable";
    }

    @RequestMapping("/outOrderAdd")
    public String outOrderAdd(Model model) {
        List<Customer> allCustomer = customerService.findAllCustomer();
        model.addAttribute("allCustomer", allCustomer);
        List<Storage> allStorage = storageMapper.findAllStorage();
        model.addAttribute("allStorage", allStorage);
        List<Product> allProduct = productService.findAllProduct();
        model.addAttribute("allProduct", allProduct);
        return "outOrderAdd";
    }

    //后台主页
    @RequestMapping("/welcome")
    public String welcome(Model model) {
        //用户总数
        int i = employeeService.countEmployee();
        model.addAttribute("employeeCount", i);
        //本月进货数
        int j = inorderService.countMonthData();
        model.addAttribute("inorderMonthCount", j);
        //合作供货商
        List<Supplier> allSuppliers = supplierService.findAllSuppliers();
        model.addAttribute("supplierCount", allSuppliers.size());
        return "welcome";
    }

    @RequestMapping("/inventoryTable")
    public String inventoryTable(Model model) {
        List<Storage> allStorage = storageMapper.findAllStorage();
        model.addAttribute("allStorage", allStorage);
        List<Product> allProduct = productService.findAllProduct();
        model.addAttribute("allProduct", allProduct);
        return "inventoryTable";
    }

    @RequestMapping("/inReport")
    public String inReport(Model model) {
        List<Storage> allStorage = storageMapper.findAllStorage();
        model.addAttribute("allStorage", allStorage);
        List<Product> allProduct = productService.findAllProduct();
        model.addAttribute("allProduct", allProduct);
        List<Supplier> allSuppliers = supplierService.findAllSuppliers();
        model.addAttribute("allSuppliers", allSuppliers);
        return "inReport";
    }

    @RequestMapping("/404")
    public String noFound() {
        return "404";
    }

    @RequestMapping("/myAccount")
    public String myAccount() {
        return "myAccount";
    }

    @RequestMapping("/inOrderShow")
    public String inOrderShow(Model model) {
        List<Storage> allStorage = storageMapper.findAllStorage();
        model.addAttribute("allStorage", allStorage);
        List<Product> allProduct = productService.findAllProduct();
        model.addAttribute("allProduct", allProduct);
        List<Supplier> allSuppliers = supplierService.findAllSuppliers();
        model.addAttribute("allSuppliers", allSuppliers);
        return "inOrderShow";
    }

    //商品列表
    @RequestMapping("/productTable")
    public String productTable() {
        return "productTable";
    }

    //商品编辑
    @RequestMapping("/productEdit")
    public String productEdit(int id, Model model) {
        Product product = productService.findAllProductByIf(Product.builder().productId(id).build()).stream().findFirst().get();
        model.addAttribute("product", product);
        return "productEdit";
    }

    //商品添加
    @RequestMapping("/productAdd")
    public String productAdd() {
        return "productAdd";
    }
}